package biz.datalk.industrialland.admin.modules.system.service.impl;

import biz.datalk.industrialland.admin.modules.system.pojo.dto.SysRoleFunctionDTO;
import biz.datalk.industrialland.admin.modules.system.pojo.vo.SysRoleFunctionVO;
import biz.datalk.industrialland.admin.modules.system.service.ISysRoleFunctionService;
import biz.datalk.industrialland.admin.modules.system.service.ISysRoleService;
import biz.datalk.industrialland.common.exception.ValidateException;
import biz.datalk.industrialland.common.util.EntityIdUtils;
import biz.datalk.industrialland.mbg.mapper.SysRoleFunctionMapper;
import biz.datalk.industrialland.mbg.pojo.po.SysRole;
import biz.datalk.industrialland.mbg.pojo.po.SysRoleFunction;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tarofang@163.com
 * @date 2020-06-01
 */
@Slf4j
@Service
public class SysRoleFunctionServiceImpl implements ISysRoleFunctionService {

    private final SysRoleFunctionMapper sysRoleFunctionMapper;

    private ISysRoleService sysRoleService;

    @Autowired
    public SysRoleFunctionServiceImpl(SysRoleFunctionMapper sysRoleFunctionMapper) {
        this.sysRoleFunctionMapper = sysRoleFunctionMapper;
    }

    @Autowired
    public void setSysRoleService(ISysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @Override
    public List<SysRoleFunctionVO> roleFuncs(SysRoleFunctionDTO sysRoleFunctionDTO) {

        List<SysRoleFunctionVO> funcs = new ArrayList<>();
        Long roleId = sysRoleFunctionDTO.getRoleId();
        if (roleId == null || roleId <= 0L) {
            return funcs;
        }

        QueryWrapper<SysRoleFunction> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq(SysRoleFunction.COL_ROLE_ID, roleId);

        List<SysRoleFunction> resList = sysRoleFunctionMapper.selectList(queryWrapper);

        if (CollectionUtils.isNotEmpty(resList)) {
            for (SysRoleFunction sysRoleFunction : resList) {
                funcs.add(new SysRoleFunctionVO(sysRoleFunction));
            }
        }

        return funcs;
    }

    private int deleteByRoleId(Long roleId) {
        // SysRoleFunctionExample example = new SysRoleFunctionExample();
        // SysRoleFunctionExample.Criteria criteria = example.createCriteria();
        // criteria.andRoleIdEqualTo(roleId);
        // return sysRoleFunctionMapper.deleteByExample(example);

        QueryWrapper<SysRoleFunction> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq(SysRoleFunction.COL_ROLE_ID, roleId);
        return sysRoleFunctionMapper.delete(queryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void roleFuncAuth(SysRoleFunctionDTO sysRoleFunctionDTO) {
        Long roleId = sysRoleFunctionDTO.getRoleId();
        if (roleId == null || roleId <= 0L) {
            throw new ValidateException("角色不存在或已被删除");
        }
        SysRole role = sysRoleService.selectById(roleId);
        if (role == null) {
            throw new ValidateException("角色不存在或已被删除");
        }

        String funcIds = StringUtils.trimToNull(sysRoleFunctionDTO.getFuncIds());
        // 清除所有权限
        if (StringUtils.isEmpty(funcIds)) {
            deleteByRoleId(roleId);
            return;
        }

        // 设置权限
        List<Long> validFuncIds = EntityIdUtils.parseIds(funcIds);

        List<SysRoleFunction> paramList = new ArrayList<>();
        for (Long validFuncId : validFuncIds) {
            SysRoleFunction sysRoleFunction = new SysRoleFunction();
            sysRoleFunction.setRoleId(roleId);
            sysRoleFunction.setFunctionId(validFuncId);
            paramList.add(sysRoleFunction);
        }

        // 删除以前权限， 再变更
        deleteByRoleId(roleId);
        for (SysRoleFunction sysRoleFunction : paramList) {
            sysRoleFunctionMapper.insertSelective(sysRoleFunction);
        }

    }
}
